package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.entity.BaseInvoiceTemplateInfoEntity;
import com.yuanfeng.base.mapper.BaseInvoiceTemplateInfoMapper;
import com.yuanfeng.base.service.BaseInvoiceTemplateInfoService;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.TokenUtils;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 *
 */
@Service
public class BaseInvoiceTemplateInfoServiceImpl extends ServiceImpl<BaseInvoiceTemplateInfoMapper, BaseInvoiceTemplateInfoEntity>
    implements BaseInvoiceTemplateInfoService{

    @Override
    public ResponseResult queryInvoiceInfoList(Map<String, Object> param) {
        String token = (String) param.get("token");
        ResponseResult tokenResult = TokenUtils.checkToken(token);
        if (tokenResult.getCode() == CommonStatusEnum.FAIL.getCode()) {
            return tokenResult;
        } else {
            param.put("userId", tokenResult.getData().toString());
        }
        try {
            IPage<BaseInvoiceTemplateInfoEntity> page = this.baseMapper.selectPage(new Query<BaseInvoiceTemplateInfoEntity>().getPage(param),
                    new LambdaQueryWrapper<BaseInvoiceTemplateInfoEntity>().eq(BaseInvoiceTemplateInfoEntity::getUserId, param.get("userId").toString())
                            .eq(param.containsKey("invoiceType"), BaseInvoiceTemplateInfoEntity::getInvoiceType, param.containsKey("invoiceType") ? param.get("invoiceType").toString() : null));

            return ResponseResult.success(PageUtils.getPage(page));
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }
    }

    @Override
    public ResponseResult insertInvoiceTmp(Map<String, Object> param) {
        String token = (String) param.get("token");
        ResponseResult tokenResult = TokenUtils.checkToken(token);
        if (tokenResult.getCode() == CommonStatusEnum.FAIL.getCode()) {
            return tokenResult;
        } else {
            param.put("userId", tokenResult.getData().toString());
        }
        if (!param.containsKey("invoiceHead")) {
            return ResponseResult.success(BizCodeEnume.INVOICE_ADDRESS_TITLE_IS_NULL.getMsg());
        }
        if (!param.containsKey("invoiceType")) {
            param.put("invoiceType", 0);
        }
        if (!param.containsKey("invoiceHeadType")) {
            param.put("invoiceHeadType", 1);
        }
        try {
            if (param.get("invoiceType").equals(1)) {
                param.put("invoiceHeadType", 2);
            }
            if (param.get("invoiceTmpId") == null) {
                Integer count = this.baseMapper.selectCount(new LambdaQueryWrapper<BaseInvoiceTemplateInfoEntity>()
                        .eq(BaseInvoiceTemplateInfoEntity::getUserId, param.get("userId").toString())
                        .eq(param.containsKey("invoiceType"), BaseInvoiceTemplateInfoEntity::getInvoiceType, param.containsKey("invoiceType") ? param.get("invoiceType").toString() : null));
                //发票模版大于10个无法添加
                if (null != count && count >= 10) {
                    //发票模版数已达上限
                    return ResponseResult.fail(BizCodeEnume.INVOICE_TEMPLATES_HAS_SUPERIOR_LIMIT);
                }
                int isDefault = Integer.parseInt(param.get("isDefault").toString());
                //如果新插入的为默认配置，则将其他的配置isDefault置为0
                if (1 == isDefault) {
                    this.baseMapper.update(null, new LambdaUpdateWrapper<BaseInvoiceTemplateInfoEntity>()
                            .eq(BaseInvoiceTemplateInfoEntity::getUserId, param.get("userId").toString())
                            .set(BaseInvoiceTemplateInfoEntity::getIsDefault, 0));
                }
                this.baseMapper.insertInvoiceTmp(param);
            } else {
                this.baseMapper.updateInvoiceTmp(param);
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }
    }

    @Override
    public ResponseResult queryInvoiceTmp(Map<String, Object> param) {
        String token = (String) param.get("token");
        ResponseResult tokenResult = TokenUtils.checkToken(token);
        if (tokenResult.getCode() == CommonStatusEnum.FAIL.getCode()) {
            return tokenResult;
        } else {
            param.put("userId", tokenResult.getData().toString());
        }
        try {
            BaseInvoiceTemplateInfoEntity invoiceTmpEntity = this.baseMapper.selectById(param.get("invoiceTmpId").toString());
            return ResponseResult.success(invoiceTmpEntity);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }

    }

    @Override
    public ResponseResult updateInvoiceTmp(Map<String, Object> param) {
        String token = (String) param.get("token");
        ResponseResult tokenResult = TokenUtils.checkToken(token);
        if (tokenResult.getCode() == CommonStatusEnum.FAIL.getCode()) {
            return tokenResult;
        } else {
            param.put("userId", tokenResult.getData().toString());
        }
        try {
            if (param.get("invoiceType").equals(1)) {
                param.put("invoiceHeadType", 2);
            }
            int isDefault = Integer.valueOf(param.get("isDefault").toString());
            //如果新插入的为默认配置，则将其他的配置isDefault置为0
            if (1 == isDefault) {
                this.baseMapper.update(null, new LambdaUpdateWrapper<BaseInvoiceTemplateInfoEntity>()
                        .eq(BaseInvoiceTemplateInfoEntity::getUserId, param.get("userId").toString())
                        .set(BaseInvoiceTemplateInfoEntity::getIsDefault, 0));
            }
            this.baseMapper.updateInvoiceTmp(param);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }

    }

    @Override
    public ResponseResult deleteInvoiceTmp(Map<String, Object> param) {
        String token = (String) param.get("token");
        ResponseResult tokenResult = TokenUtils.checkToken(token);
        if (tokenResult.getCode() == CommonStatusEnum.FAIL.getCode()) {
            return tokenResult;
        } else {
            param.put("userId", tokenResult.getData().toString());
        }
        try {
            this.baseMapper.delete(new LambdaQueryWrapper<BaseInvoiceTemplateInfoEntity>()
                    .eq(BaseInvoiceTemplateInfoEntity::getUserId, param.get("userId").toString())
                    .eq(BaseInvoiceTemplateInfoEntity::getInvoiceTmpId, param.get("invoiceTmpId").toString()));
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.fail();
        }
    }
}




